package com.lkk.cg.action;

import com.lkk.cg.base.BaseAction;
import com.lkk.cg.domain.Article;
import com.lkk.cg.domain.Reply;
import com.lkk.cg.util.FileUtils;
import com.lkk.cg.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * Created by lkk on 2014/12/13.
 */
@Controller
@Scope("prototype")
public class ArticleAction extends BaseAction<Article> {

    private File imageFile;
    private String imageFileFileName;
    private String imageFileContentType;
    private File attachmentFile;
    private String attachmentFileFileName;
    private String attachmentFileContentType;
    private InputStream imageStream;
    private InputStream attachmentStream;

    //Page info
    private String sortBy;
    private String asc;
    private String filterString;

    //comment
    public String comment;

    //=================Actions=================

    /**
     * 新建页面
     *
     * @return
     */
    public String addUI() {
        return "addUI";
    }

    /**
     * 新建
     *
     * @return
     */
    @InputConfig(resultName = "addUI")
    public String add() {

        model.setCreateTime(new Date(System.currentTimeMillis()));
        model.setImage(FileUtils.getBytesFromFile(imageFile));
        model.setImageFileName(imageFileFileName);
        model.setImageContentType(imageFileContentType);
        model.setAttachment(FileUtils.getBytesFromFile(attachmentFile));
        model.setAttachmentFileName(attachmentFileFileName);
        model.setAttachmentContentType(attachmentFileContentType);
        model.setAuthor(getCurrentUser());

        log.debug("save new article...");
        articleService.save(model);

        return "toDetails";
    }

    /**
     * 文章列表
     *
     * @return
     */
    public String list() {
        QueryHelper queryHelper = new QueryHelper(Article.class, "a");
        queryHelper.addOrderProperty("id".equals(sortBy), "a.id", "asc".equals(asc));
        queryHelper.addOrderProperty("time".equals(sortBy), "a.createTime", "asc".equals(asc));
        queryHelper.addOrderProperty("download".equals(sortBy), "a.downloadCount", "asc".equals(asc));
        queryHelper.addCondition(filterString != null, "a.title like ?", "%" + filterString + "%");
        queryHelper.preparePageBean(articleService, getPageNum(), getPageSize());

        return "list";
    }

    /**
     * 文章详情
     *
     * @return
     */
    public String details() {
        Article article = articleService.getById(model.getId());
        ActionContext.getContext().getValueStack().push(article);
        return "details";
    }

    /**
     * 评论文章
     *
     * @return
     */
    public String comment() {
        Article article = articleService.getById(model.getId());
        Reply reply = new Reply();
        reply.setContent(comment);
        reply.setAuthor(getCurrentUser());
        reply.setCreateTime(new Date());
        reply.setArticle(article);
        replyService.save(reply);

        return "toDetails";
    }

    /**
     * 获得指定文章的图片
     *
     * @return
     */
    public String getImage() {
        Article article = articleService.getById(model.getId());
        imageStream = new ByteArrayInputStream(article.getImage());
        imageFileContentType = article.getImageContentType();
        imageFileFileName = article.getImageFileName();
        return "image";
    }

    /**
     * 获得指定文章的图片
     *
     * @return
     */
    public String getAttachment() throws UnsupportedEncodingException {
        Article article = articleService.getById(model.getId());
        article.setDownloadCount(article.getDownloadCount() + 1);
        articleService.update(article);

        attachmentStream = new ByteArrayInputStream(article.getAttachment());
        attachmentFileContentType = article.getAttachmentContentType();
//        attachmentFileFileName = article.getAttachmentFileName();
        attachmentFileFileName = java.net.URLEncoder.encode(article.getAttachmentFileName(), "UTF-8");

        dailyStatisticsService.increment();

        return "attachment";
    }

    //=================Getters and Setters=================

    public File getImageFile() {
        return imageFile;
    }

    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }

    public String getImageFileFileName() {
        return imageFileFileName;
    }

    public void setImageFileFileName(String imageFileFileName) {
        this.imageFileFileName = imageFileFileName;
    }

    public String getImageFileContentType() {
        return imageFileContentType;
    }

    public void setImageFileContentType(String imageFileContentType) {
        this.imageFileContentType = imageFileContentType;
    }

    public File getAttachmentFile() {
        return attachmentFile;
    }

    public void setAttachmentFile(File attachmentFile) {
        this.attachmentFile = attachmentFile;
    }

    public String getAttachmentFileFileName() {
        return attachmentFileFileName;
    }

    public void setAttachmentFileFileName(String attachmentFileFileName) {
        this.attachmentFileFileName = attachmentFileFileName;
    }

    public String getAttachmentFileContentType() {
        return attachmentFileContentType;
    }

    public void setAttachmentFileContentType(String attachmentFileContentType) {
        this.attachmentFileContentType = attachmentFileContentType;
    }

    public InputStream getImageStream() {
        return imageStream;
    }

    public void setImageStream(InputStream imageStream) {
        this.imageStream = imageStream;
    }

    public InputStream getAttachmentStream() {
        return attachmentStream;
    }

    public void setAttachmentStream(InputStream attachmentStream) {
        this.attachmentStream = attachmentStream;
    }

    //Page info


    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

    public String isAsc() {
        return asc;
    }

    public void setAsc(String asc) {
        this.asc = asc;
    }

    public String getFilterString() {
        return filterString;
    }

    public void setFilterString(String filterString) {
        this.filterString = filterString;
    }
}
